#include "stdafx.h"
#include "Time.h"
#include "Timer.h"

using namespace unigame;

Time* Time::s_instance = 0;

Time::Time():
m_deltaTime(0),
m_pauseTime(-1),
m_realDeltaTime(0),
m_timeScale(1),
m_realTime(0)
{
	m_lastTime = clock();
}

Time::~Time()
{
}

void Time::update()
{
	clock_t time = clock();
    m_realDeltaTime = (int64u)((time - m_lastTime) / (float)CLOCKS_PER_SEC * 1000);
    m_realTime += m_realDeltaTime;
    if(m_pauseTime > 0)
    {
		m_realDeltaTime -= m_pauseTime;
        m_pauseTime = -1;
    }
	//printf("%i\n", m_realDeltaTime);
	m_deltaTime = m_realDeltaTime * m_timeScale / 1000.0f;
	m_lastTime = time;
}

void Time::onPause()
{
    m_pauseStartTime = clock();
}

void Time::onResume()
{
    m_pauseTime = (clock() - m_pauseStartTime) / CLOCKS_PER_SEC * 1000;
}